var t = require;
var e = module;
var o = exports;
Object.defineProperty(o, "__esModule", {value: !0}), (o.wxGameServer = o.WXGSRole = o.RoomState = void 0);
var n,
    r = t("Platforms");
(function (t) {
    (t[(t.online = 0)] = "online"),
        (t[(t.inTeam = 1)] = "inTeam"),
        (t[(t.gameStart = 2)] = "gameStart"),
        (t[(t.gameEnd = 3)] = "gameEnd"),
        (t[(t.busy = 4)] = "busy");
})(o.RoomState || (o.RoomState = {})),
    (function (t) {
        (t[(t.member = 0)] = "member"), (t[(t.owner = 1)] = "owner");
    })((n = o.WXGSRole || (o.WXGSRole = {})));
var a = (function () {
    function t() {
        var t = this;
        (this.invite = function (e) {
            t.inviteCallback && t.inviteCallback(e.data);
        }),
            (this.roomInfoChange = function (e) {
                t.updateRoomInfo(e);
            });
    }
    return (
        (t.prototype.init = function (t) {
            "undefined" != typeof wx &&
                ((this.needUserInfo = t), wx.getGameServerManager && (this.server = wx.getGameServerManager()));
        }),
        (t.prototype.getSelf = function () {
            for (var t = 0, e = this.roomInfo.memberList; t < e.length; t++) {
                var o = e[t];
                if (o.clientId == this.clientId) return o;
            }
            return null;
        }),
        (t.prototype.getOpponent = function () {
            for (var t = 0, e = this.roomInfo.memberList; t < e.length; t++) {
                var o = e[t];
                if (o.clientId != this.clientId) return o;
            }
            return null;
        }),
        (t.prototype.getOwner = function () {
            for (var t = 0, e = this.roomInfo.memberList; t < e.length; t++) {
                var o = e[t];
                if (o.role == n.owner) return o;
            }
            return null;
        }),
        (t.prototype.getMember = function () {
            for (var t = 0, e = this.roomInfo.memberList; t < e.length; t++) {
                var o = e[t];
                if (o.role == n.member) return o;
            }
            return null;
        }),
        (t.prototype.成功 = function (t, e, o, n) {
            n && n();
        }),
        (t.prototype.失败 = function (t, e, o, n, r, a) {
            console.error(t + " " + e + " " + o + " " + JSON.stringify(n)),
                a && n && 1001 == n.errCode
                    ? this.login(
                          function () {
                              a();
                          },
                          function () {
                              r && r();
                          }
                      )
                    : r && r();
        }),
        (t.prototype.loginAndStartStateService = function (t, e) {
            var o = this;
            this.login(
                function () {
                    o.startStateService(
                        t,
                        function () {
                            (o.isLogin = !0), e && e(!0);
                        },
                        function () {
                            e && e(!1);
                        }
                    );
                },
                function () {
                    e && e(!1);
                }
            );
        }),
        (t.prototype.login = function (t, e) {
            var o = this;
            if (this.server) {
                this.server.login().then(
                    function () {
                        o.成功("login", "登录", null, t);
                    },
                    function (t) {
                        o.失败("login", "登录", "失败", t, e);
                    }
                );
            }
        }),
        (t.prototype.logout = function (t, e) {
            var o = this;
            if (this.server) {
                (this.isLogin = !1),
                    this.server.logout().then(
                        function () {
                            o.成功("logout", "登出", null, t);
                        },
                        function (t) {
                            o.失败("logout", "登出", "失败", t, e);
                        }
                    );
            }
        }),
        (t.prototype.startStateService = function (t, e, o) {
            var n = this,
                r = "startStateService";
            this.server.startStateService({
                userState: t,
                success: function (t) {
                    0 == t.errCode ? n.成功(r, "开启状态管理服务", t, e) : n.失败(r, "开启状态管理服务", "异常", t, o);
                },
                fail: function (t) {
                    n.失败(r, "开启状态管理服务", "失败", t, o);
                }
            });
        }),
        (t.prototype.setState = function (t, e, o) {
            var n = this;
            if (this.server) {
                this.server.setState({
                    userState: t,
                    success: function (r) {
                        0 == r.errCode
                            ? n.成功("setState", "更新状态", r, e)
                            : n.失败("setState", "更新状态", "失败", r, o, function () {
                                  n.setState(t, e, o);
                              });
                    },
                    fail: function (r) {
                        n.失败("setState", "更新状态", "失败", r, o, function () {
                            n.setState(t, e, o);
                        });
                    }
                });
            }
        }),
        (t.prototype.setInviteData = function (t) {
            if (this.server) {
                return !!this.server.setInviteData(t);
            }
        }),
        (t.prototype.createRoomAndGetRoomInfo = function (t, e, o) {
            var n = this;
            this.createRoom(
                t,
                function () {
                    n.getRoomInfo(function () {
                        n.onRoomInfoChange(), e && e();
                    }, o);
                },
                function () {
                    wx.showModal({
                        title: "提示",
                        content: "网络连接错误，请检查网络！",
                        success: function () {
                            n.login(
                                function () {
                                    n.createRoomAndGetRoomInfo(t, e, o);
                                },
                                function () {
                                    n.createRoomAndGetRoomInfo(t, e, o);
                                }
                            );
                        },
                        showCancel: !1,
                        confirmText: "更 新"
                    });
                }
            );
        }),
        (t.prototype.createRoom = function (t, e, o) {
            var n = this;
            this.server.createRoom({
                maxMemberNum: 2,
                needUserInfo: this.needUserInfo,
                memberExtInfo: t,
                success: function (r) {
                    0 == r.errCode
                        ? r.data && r.data.accessInfo && r.data.clientId > 0
                            ? ((n.accessInfo = r.data.accessInfo),
                              (n.clientId = r.data.clientId),
                              n.成功("createRoom", "创建房间", r, e))
                            : n.失败("createRoom", "创建房间", "失败", r, o)
                        : n.失败("createRoom", "创建房间", "失败", r, o, function () {
                              n.createRoom(t, e, o);
                          });
                },
                fail: function (r) {
                    n.失败("createRoom", "创建房间", "失败", r, o, function () {
                        n.createRoom(t, e, o);
                    });
                }
            });
        }),
        (t.prototype.getRoomInfo = function (t, e) {
            var o = this,
                n = "getRoomInfo";
            this.server.getRoomInfo({
                success: function (r) {
                    0 == r.errCode
                        ? r.data && r.data.roomInfo
                            ? (o.updateRoomInfo(r.data.roomInfo), o.成功(n, "获取房间详情", r, t))
                            : o.失败(n, "获取房间详情", "失败", r, e)
                        : o.失败(n, "获取房间详情", "失败", r, e, function () {
                              o.getRoomInfo(t, e);
                          });
                },
                fail: function (r) {
                    o.失败(n, "获取房间详情", "失败", r, e, function () {
                        o.getRoomInfo(t, e);
                    });
                }
            });
        }),
        (t.prototype.joinRoomAndGetRoomInfo = function (t, e, o, n) {
            var r = this,
                a = function () {
                    r.joinRoom(
                        t,
                        e,
                        function () {
                            r.getRoomInfo(function () {
                                r.onRoomInfoChange(), o && o();
                            }, n);
                        },
                        n
                    );
                };
            this.roomInfo ? this.leaveRoom(a, a) : a();
        }),
        (t.prototype.joinRoom = function (t, e, o, n) {
            var r = this;
            (this.accessInfo = t),
                this.server.joinRoom({
                    accessInfo: this.accessInfo,
                    memberExtInfo: e,
                    success: function (a) {
                        0 == a.errCode
                            ? a.data && a.data.clientId
                                ? ((r.clientId = a.data.clientId), r.成功("joinRoom", "加入房间", a, o))
                                : r.失败("joinRoom", "加入房间", "失败", a, n)
                            : r.失败("joinRoom", "加入房间", "失败", a, n, function () {
                                  r.joinRoom(t, e, o, n);
                              });
                    },
                    fail: function (a) {
                        r.失败("joinRoom", "加入房间", "失败", a, n, function () {
                            r.joinRoom(t, e, o, n);
                        });
                    }
                });
        }),
        (t.prototype.leaveRoom = function (t, e) {
            this.roomInfo &&
                this.roomInfo.memberList &&
                (this.getSelf().role == n.owner ? this.ownerLeaveRoom(t, e) : this.memberLeaveRoom(t, e),
                (this.roomInfo = null),
                this.offRoomInfoChange());
        }),
        (t.prototype.memberLeaveRoom = function (t, e) {
            var o = this,
                n = "memberLeaveRoom";
            this.server.memberLeaveRoom({
                accessInfo: this.accessInfo,
                success: function (r) {
                    0 == r.errCode
                        ? o.成功(n, "普通成员退房", r, t)
                        : o.失败(n, "普通成员退房", "失败", r, e, function () {
                              o.memberLeaveRoom(t, e);
                          });
                },
                fail: function (r) {
                    o.失败(n, "普通成员退房", "失败", r, e, function () {
                        o.memberLeaveRoom(t, e);
                    });
                }
            });
        }),
        (t.prototype.ownerLeaveRoom = function (t, e) {
            var o = this,
                n = "ownerLeaveRoom";
            this.server.ownerLeaveRoom({
                accessInfo: this.accessInfo,
                assignToMinPosNum: !0,
                success: function (r) {
                    0 == r.errCode
                        ? o.成功(n, "房主退房", r, t)
                        : o.失败(n, "房主退房", "失败", r, e, function () {
                              o.ownerLeaveRoom(t, e);
                          });
                },
                fail: function (r) {
                    o.失败(n, "房主退房", "失败", r, e, function () {
                        o.ownerLeaveRoom(t, e);
                    });
                }
            });
        }),
        (t.prototype.onInvite = function (t) {
            (this.inviteCallback = t), this.server.onInvite(this.invite);
        }),
        (t.prototype.offInvite = function () {
            (this.inviteCallback = null), this.server.offInvite(this.invite);
        }),
        (t.prototype.updateRoomInfo = function (t) {
            var e = this;
            r.default.delays.delay({
                time: 0,
                action: function () {
                    t.memberList
                        ? ((e.roomInfo = t),
                          console.log("roomInfo.memberList.length=" + e.roomInfo.memberList.length),
                          e.onUpdateRoomInfo && e.onUpdateRoomInfo())
                        : r.default.platformCtrl.toast("房间详情异常 " + JSON.stringify(t));
                }
            });
        }),
        (t.prototype.onRoomInfoChange = function () {
            this.server.onRoomInfoChange(this.roomInfoChange);
        }),
        (t.prototype.offRoomInfoChange = function () {
            (this.onUpdateRoomInfo = null), this.server.offRoomInfoChange(this.roomInfoChange);
        }),
        t
    );
})();
o.wxGameServer = new a();
